package pers.kui.reader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import pers.kui.reader.entity.Book;
import pers.kui.reader.entity.Evaluation;
import pers.kui.reader.entity.Member;
import pers.kui.reader.mapper.BookMapper;
import pers.kui.reader.mapper.EvaluationMapper;
import pers.kui.reader.mapper.MemberMapper;
import pers.kui.reader.service.EvaluationService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liu
 * @date 2021/8/12
 * @apiNote
 */
@Service("evaluationService")
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public class EvaluationServiceImpl implements EvaluationService {
    @Resource
    private EvaluationMapper evaluationMapper;
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private BookMapper bookMapper;

    /**
     * 按图书编号查询有效短评
     *
     * @param bookId 图书编号
     * @return 评论列表
     */
    @Override
    public List<Evaluation> selectByBookId(Long bookId) {
        Book book = bookMapper.selectById(bookId);
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<Evaluation>();
        queryWrapper.eq("book_id", bookId);
        queryWrapper.eq("state", "enable");
        queryWrapper.orderByDesc("create_time");
        List<Evaluation> evaluationList = evaluationMapper.selectList(queryWrapper);
        for (Evaluation eva : evaluationList) {
            Member member = memberMapper.selectById(eva.getMemberId());
            eva.setMember(member);
            eva.setBook(book);
        }
        return evaluationList;
    }

    /**
     * 分页查询短评
     *
     * @param categoryId 分类编号
     * @param order      排序方式
     * @param page       页号
     * @param rows       每页记录数
     * @return 分页对象
     */
    @Transactional
    @Override
    public IPage<Evaluation> paging(Long categoryId, String order, Integer page, Integer rows) {
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<Evaluation>();
        List<Evaluation> evaluationList = evaluationMapper.selectList(queryWrapper);
        for (Evaluation eva : evaluationList) {
            //Member member = memberMapper.selectById(eva.getMemberId());
            //Book book = bookMapper.selectById(eva.getBookId());
            eva.setBook(bookMapper.selectById(eva.getBookId()));
            eva.setMember(memberMapper.selectById(eva.getMemberId()));

        }
        List<Evaluation> evaluationListCopy = evaluationMapper.selectList(queryWrapper);
        for (Evaluation eva : evaluationListCopy) {
            //Member member = memberMapper.selectById(eva.getMemberId());
            //Book book = bookMapper.selectById(eva.getBookId());

            System.out.println(eva.getBook().getBookName());
        }
        Page<Evaluation> p = new Page<Evaluation>(page, rows);
        queryWrapper.orderByDesc("create_time");
        IPage<Evaluation> pageObject = evaluationMapper.selectPage(p, queryWrapper);
        return pageObject;
    }
}





